import { createRouter, createWebHistory} from 'vue-router';
import cookies from 'vue-cookies';

const publicRoutes = [
    {
      path: '/login',   
      name: 'login',
      component: () => import('../views/login.vue'),
      // 自定义键值
      meta: {
        title: '管理员登录'
      }
    }, 
    {
        path: '/',
        name: 'index',
        component: () => import('../views/index.vue'),
        children: [
            {
                path: 'home',
                name: 'home',
                component: () => import('../views/home.vue'),
                meta: {
                    title: '首页'
                }
            },
            {
                path: 'user',
                name: 'user',
                component: () => import('../views/user.vue'),
                meta: {
                    title: '用户管理'
                }
            },
            {
                path: 'forum',
                name: 'forum',
                component: () => import('../views/forum.vue'),
                meta: {
                    title: '帖子管理'
                }
            }
        ]
    }
]
  
export const asyncRoutes = [
    // {
    //     path: '/admin',   
    //     component: () => import('../pages/adminLayoutView.vue'),
    //     meta: { requiresAuth: true },
    //     children: []
    // }
]


// 创建router
const router = createRouter({
    routes: [
      ...publicRoutes,
      ...asyncRoutes
    ],
    history: createWebHistory()
})
  
// 路由守卫（路由拦截器）
router.beforeEach((to, from, next) => {
    if (to.meta.title) { // 判断是否有标题
        document.title = to.meta.title;
    } else {
        document.title = 'bbs论坛管理系统';
    }
    let user = cookies.get('bbs-admin'); // 把登录信息放在cookies中，可以通过cookies获取用户信息
    if(to.name === 'login') {
        next();
    } else if(user) {
        // 如果已经登录
        next();
    } else {
        // 否则跳转到登录页
        next({
            path: '/login'
        })
    }
})
router.afterEach( (to, from) => {
})

export default router;